package com.example.invoicing.controller;

import com.example.invoicing.pojo.InboundOutbound;
import com.example.invoicing.pojo.PageResult;
import com.example.invoicing.pojo.User;
import com.example.invoicing.service.UserService;
import com.example.invoicing.util.DateUtil;
import com.example.invoicing.util.ReturnUtil;
import com.example.invoicing.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.ui.ModelMap;
import org.springframework.util.DigestUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class UserJsonController {
    @Autowired
    private UserService userService;

    //用户注册方法
    @ResponseBody
    @RequestMapping(value = "/member/userRegister",method = RequestMethod.POST)
    public ModelMap userRegister(@Validated @RequestBody User user, BindingResult result){
        try {

            //是否为空检验
            if(StringUtils.isEmpty(user.getUserLoginName())){
                return ReturnUtil.error("用户名不能为空");
            }
            if(StringUtils.isEmpty(user.getUserLoginPwd())){
                return ReturnUtil.error("密码不能为空");
            }
            if(StringUtils.isEmpty(user.getUserLevel())){
                return ReturnUtil.error("用户类型不能为空");
            }

            if(StringUtils.isEmpty(user.getUserName())){
                user.setUserName(null);
            }

            if(StringUtils.isEmpty(user.getUserBrithday())){
                user.setUserBrithday(null);
            }

            if(StringUtils.isEmpty(user.getUserAddress())){
                user.setUserAddress(null);
            }

            if(StringUtils.isEmpty(user.getUserTel())){
                user.setUserTel(null);
            }

            //查询该注册的用户是否存在
            User user1 = userService.getByUser(user);
            if (user1 != null) {
                return ReturnUtil.error("用户名已存在");
            }

            user.setId(UuidUtil.getUUID());
            //先保存明文密码
            user.setPlaintPwd(user.getUserLoginPwd());
            //将密码进行md5加密
            String password = DigestUtils.md5DigestAsHex(user.getUserLoginPwd().getBytes());
            user.setUserLoginPwd(password);
            //获取到当前时间
            user.setCreateTime(DateUtil.getCurrentTime());
            //随机生成一串数字
            user.setCreateUserId(UuidUtil.runVerifyCode(10));

            int count = userService.insertSelective(user);
            if (count == 0){
                return ReturnUtil.success("用户注册失败");
            }

            return ReturnUtil.success("注册用户信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }

    }

    //用户登录方法
    @ResponseBody
    @RequestMapping(value = "/member/userLogin",method = RequestMethod.POST)
    public ModelMap userLogin(@Validated @RequestBody User user, BindingResult result){
        try {
            if(StringUtils.isEmpty(user.getUserLoginName())){
                return ReturnUtil.error("用户名不能为空");
            }

            if(StringUtils.isEmpty(user.getUserLoginPwd())){
                return ReturnUtil.error("密码不能为空");
            }

            //根据课程用户信息名判断该用户是否存在
            User userBean = userService.getByUser(user);
            if (userBean == null){
                return ReturnUtil.error("用户名不存在");
            }

            //校验密码是否正确
            if (!(user.getUserLoginPwd().equals(userBean.getPlaintPwd()))){
                return ReturnUtil.error("密码错误");
            }

            userBean.setPlaintPwd("");

            return ReturnUtil.success("获取用户信息成功", userBean);

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //修改用户信息
    @ResponseBody
    @RequestMapping(value = "/member/userInformation",method = RequestMethod.POST)
    public ModelMap userInformation(@Validated @RequestBody User user, BindingResult result){
        try {
            if(StringUtils.isEmpty(user.getId())){
                return ReturnUtil.error("用户ID不能为空");
            }

            if(StringUtils.isEmpty(user.getUserName())){
                user.setUserName(null);
            }

            if(StringUtils.isEmpty(user.getUserBrithday())){
                user.setUserBrithday(null);
            }

            if(StringUtils.isEmpty(user.getUserAddress())){
                user.setUserAddress(null);
            }

            if(StringUtils.isEmpty(user.getUserTel())){
                user.setUserTel(null);
            }

            //当传入的密码不为空时，进行的是修改密码操作
            if (!StringUtils.isEmpty(user.getUserLoginPwd())){
                User userBean = userService.selectByPrimaryKey(user.getId());

                //校验邮箱是否正确
                if (!userBean.getUserEmail().equals(user.getUserEmail())){
                    return ReturnUtil.error("您的邮箱不正确");
                }

                user.setPlaintPwd(user.getUserLoginPwd());
                //将密码进行md5加密
                String password = DigestUtils.md5DigestAsHex(user.getUserLoginPwd().getBytes());
                user.setUserLoginPwd(password);
            }

            //更新修改时间
            user.setUpdateTime(DateUtil.getCurrentTime());

            int count = userService.updateByPrimaryKeySelective(user);
            if (count == 0){
                return ReturnUtil.error("修改用户信息失败");
            }

            return ReturnUtil.success("修改用户信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //用户忘记密码修改
    @ResponseBody
    @RequestMapping(value = "/member/updatePaswd",method = RequestMethod.POST)
    public ModelMap updatePaswd(@Validated @RequestBody User user, BindingResult result){
        try {
            if(StringUtils.isEmpty(user.getUserLoginName())){
                return ReturnUtil.error("账号不能为空");
            }

            //验证用户是否存在，以便修改密码
            User userBean = userService.getByUser(user);
            if (userBean == null){
                return ReturnUtil.error("用户名不存在");
            }

            if (!userBean.getUserEmail().equals(user.getUserEmail())){
                return ReturnUtil.error("您的邮箱不正确");
            }

            user.setId(userBean.getId());
            user.setPlaintPwd(user.getUserLoginPwd());
            //md5加密
            String password = DigestUtils.md5DigestAsHex(user.getUserLoginPwd().getBytes());
            user.setUserLoginPwd(password);

            user.setUpdateTime(DateUtil.getCurrentTime());

            int count = userService.updateByPrimaryKeySelective(user);
            if (count == 0){
                return ReturnUtil.error("修改用户信息失败");
            }

            return ReturnUtil.success("修改用户信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //用户ID查信息
    @ResponseBody
    @RequestMapping(value = "/member/getUserInfo",method = RequestMethod.POST)
    public ModelMap getUserInfo(@Validated @RequestBody User user, BindingResult result){
        try {

            if(StringUtils.isEmpty(user.getId())){
                return ReturnUtil.error("用户ID不能为空");
            }

            User user1 = userService.selectByPrimaryKey(user.getId());
            if (user1 == null){
                return ReturnUtil.error("获取用户信息失败");
            }

            return ReturnUtil.success("获取用户信息成功", user1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }


    //删除一个用户
    @ResponseBody
    @RequestMapping(value = "/member/deleteUser",method = RequestMethod.POST)
    public ModelMap deleteUser(@Validated @RequestBody User user, BindingResult result){
        try {

            if(StringUtils.isEmpty(user.getId())){
                return ReturnUtil.error("用户ID不能为空");
            }

            int count = userService.deleteByPrimaryKey(user.getId());
            if (count == 0){
                return ReturnUtil.error("删除用户失败");
            }

            return ReturnUtil.success("删除用户成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //集合于用户分页查询，所有用户查询，用户类型查询，用户账号和名字模糊查询
    @ResponseBody
    @RequestMapping(value = "/member/allUserList",method = RequestMethod.POST)
    public ModelMap allUserList(@Validated @RequestBody User user, BindingResult result){
        try {

            List<User> user1 = userService.getAllUserList(user);
            if (user1.size() == 0){
                return ReturnUtil.error("查询失败,无该数据");
            }

            //查询数据有多少条
            int countLike = userService.queryCountLike(user);

            //加载实体将数据返回给用户
            PageResult pageResult = new PageResult();
            if (user.getPageResult() != null){
                pageResult.setCurrentPage(user.getPageResult().getCurrentPage());
                pageResult.setPageSize(user.getPageResult().getPageSize());

            }

            pageResult.setTotalSize(countLike);
            pageResult.setItems(user1);

            return ReturnUtil.success("查询所有用户成功", pageResult);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }



}
